{% extends "manage/layout.html" %}

{% set page_title = '机构列表' %}

{% block content %}
<style>
.tab{
    position: absolute;
    margin-left: 40px;
    margin-top: 10px;
}
</style>
<div class="tab">
    <a type="button" class="btn btn-primary btn-success" href="{{ url_for('manage.list_schools') }}">资料视图</a>
    <a type="button" class="btn btn-primary" href="{{ url_for('manage.schools_operation_list') }}">运营视图</a>
</div>
<div class="m-content">
    <div class="table-top">
        {% if current_user.has_permission('EDIT') -%}
        <a type="button" class="btn btn-default" id="createnew" href="{{ url_for('manage.create_school') }}">新建</a>
        {%- endif %}
        {# 去掉这里的功能。问题号：COS-905
        <a type="button" class="btn btn-default" href="javascript:;" onclick="location.href='{{ url_for('manage.export_schools_excel') }}'">导出Excel</a>
        #}
        <div class="search_form pull-right" style="margin-right:40px;">
            <form class="form-inline" action="{{ url_for('manage.search') }}" method="GET">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 330px" id="keywords" name="keywords" placeholder="机构名称、代码、地址、联系人、手机号" value="{% if request.args.get('keywords') %}{{ request.args.get('keywords') }}{% endif %}"/>
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>
        </div>
    </div>
    <table data-toggle="table" class="table table-striped table-condensed table-bordered">
        <thead>
            <tr>
                <th data-sortable="true">序号</th>
                <th>机构名称</th>
                <th data-sortable="true">地区</th>
                <th data-sortable="true">联系人</th>
                <th>联系电话</th>
                <th data-sortable="true">类型</th>
                <th data-sortable="true">创建于</th>
                <th>教室管理</th>
                {% if current_user.has_permission('EDIT') -%}
                <th>管理</th>
                {%- endif %}
            </tr>
        </thead>
        <tbody>
            {% if schools %}
                {% for school in schools %}
                <tr id="{{ school.id }}">
                    <td>{{ school.id }}</td>
                    <td>
                        {{ school.name }}<br />
                        <span class="school-code">{{ school.code }}</span>
                    </td>
                    <td>{{ school.area }}</td>
                    <td>{{ school.contact_name }}</td>
                    <td>{{ school.phone }}</td>
                    <td>{{ school.school_type }}</td>
                    <td>{{ school.created_at }}</td>
                    <td>
                        {%- if school.classrooms %}
                            {% for cr in school.classrooms %}
                            <a style="width:36px;" class="btn btn-sm btn-default" href="{{ url_for('manage.list_classrooms_by_sid', school_id=school.id) }}">{{ cr.capacity }}</a>
                            {% if not loop.index % 4 %}
                            <br />
                            {% endif %}
                            {% endfor %}
                        {% else %}
                        <a style="width:45px;color:#393;" class="btn btn-sm btn-default" href="{{ url_for('manage.list_classrooms_by_sid', school_id=school.id) }}">new</a>
                        {% endif %}
                    </td>
                    {% if current_user.has_permission('EDIT') -%}
                    <td>
                        <a href="{{ url_for('manage.edit_school', sid=school.id) }}"><span class="glyphicon-edit glyphicon" title="编辑"></span></a>
                        <a href="#"><span class="glyphicon glyphicon-trash" title="删除"></span></a>
                        <a href="{{ url_for('manage.goto_school', school_id=school.id) }}" target="_blank"><span class="glyphicon glyphicon-log-in" title="跳转"></span></a>
                    </td>
                    {%- endif %}
                </tr>
                {% endfor %}
            {% endif %}
        </tbody>
    </table>
</div>
<div class="modal">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">确认删除</h4>
            </div>
            <div class="modal-body">
                <button class="btn btn-danger">确认</button>
                <button class="btn btn-default">取消</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
$(function(){
    // click trash
    var muti_delete = false;
    var school_id;

    $('span.glyphicon-trash').bind('click',function(){
        school_id = $(this).parents('tr').attr('id');
        $('div.modal').modal('show');
    });

    // click modal alert confirm button
    $('.modal-body button.btn-danger').bind('click',function(){
        var url = '/manage/schools/delete/' + school_id;
        if (!muti_delete){
            if(!school_id) return;

            $.post(url, function(data){
                if (data['code']!=0){
                    alert(data['msg']);
                }
                window.location.href = "{{ url_for('manage.list_schools') }}";
            }, 'json');
        }

    });

    $('.modal button').bind('click',function(){
        $('.modal').modal('hide');
    });

});

</script>
{% endblock %}
